package com.example.meizu002.controller;

import com.example.meizu002.entity.Phone;
import com.example.meizu002.service.PhoneService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("Phone")
@CrossOrigin
@Slf4j
@Transactional
public class PhoneController {
    @Autowired
    private PhoneService phoneService;

    @GetMapping("/findAll")

    public Map<String, Object> findAll() {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Phone> phones = phoneService.findAll();
            map.put("records", phones);
            map.put("state", true);
            map.put("code", 200);
            map.put("msg", "手机信息查询成功!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("code", 500);
            map.put("msg", "手机信息查询失败!");
        }
        return map;
    }

    @GetMapping("/deletePhone")
    public Map<String, Object> deletePhone(int id) {
        log.info("删除商品的id:[{}]", id);
        Map<String, Object> map = new HashMap<>();
        try {
            phoneService.deletePhone(id);
            map.put("state", true);
            map.put("code", 200);
            map.put("msg", "删除手机信息成功!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("code", 500);
            map.put("msg", "删除手机信息失败!");
        }
        return map;
    }

    @GetMapping("/findOne")
    public Map<String, Object> findOne(int id) {
        Map<String, Object> map = new HashMap<>();
        try {
            Phone phone = phoneService.findOne(id);
            map.put("records", phone);
            map.put("state", true);
            map.put("code", 200);
            map.put("msg", "手机信息查询成功!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("code", 500);
            map.put("msg", "手机信息查询失败!");
        }
        return map;
    }

    @PostMapping("/addPhone")
    public Map<String, Object> addPhone(Phone phone) {
        Map<String, Object> map = new HashMap<>();
        try {
            phoneService.addPhone(phone);
            map.put("state", true);
            map.put("code", 200);
            map.put("msg", "手机信息添加成功!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("code", 500);
            map.put("msg", "手机信息添加失败!");
        }
        return map;
    }

    @PostMapping("/updatePhone")
    public Map<String, Object> updatePhone(Phone phone) {
        Map<String, Object> map = new HashMap<>();
        try {
            phoneService.updatePhone(phone);
            map.put("state", true);
            map.put("code", 200);
            map.put("msg", "手机信息修改成功!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("code", 500);
            map.put("msg", "手机信息修改失败!");
        }
        return map;
    }
}
